package com.sphd.emanu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationItem;
import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView;
import com.luseen.luseenbottomnavigation.BottomNavigation.OnBottomNavigationItemClickListener;
import com.sphd.emanu.adapter.MyViewPagerAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.contacts.ContactsFragment;
import com.sphd.emanu.fragment.data.DataFragment;
import com.sphd.emanu.fragment.dispose.DisposeFragment;
import com.sphd.emanu.fragment.mine.MineFragment;
import com.sphd.emanu.fragment.work.WorkFragment;
import com.sphd.emanu.interfaces.LetterAtoF;
import com.sphd.emanu.pojo.login.User;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGNotifaction;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.XGPushNotifactionCallback;
import com.wzutils.JsonUtil;
import com.wzutils.L;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 主界面
 *
 * @Author Hu
 * @Time 2017/4/1 -- 11:32.
 * @Email 87683202@qq.com
 */
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, LetterAtoF {


    @BindView(R.id.bottomNavigation)
    BottomNavigationView bottomNavigation;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    DataFragment dataFragment;
    WorkFragment workFragment;
    DisposeFragment disposeFragment;
    ContactsFragment contactsFragment;
    MineFragment mineFragment;
    List<Fragment> fragments;
    MyViewPagerAdapter myViewPagerAdapter;
    int[] image, color;
    String[] strings;
    Intent intent;
    private static final String TAG = "MainActivity";
    Bundle bundle;
    String roleid = null;
    User user;
    boolean isManager = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView(savedInstanceState);
        initEvent();
    }


    private void initEvent() {
        myVp.addOnPageChangeListener(this);
    }


    private void initView(Bundle savedInstanceState) {
        bundle = getIntent().getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        roleid = user.getRoleid();
        //判断登陆者的身份是超管还是店长
        if (roleid.equals("1") || roleid.equals("2")) {
            isManager = true;
        }

        initRes();
        initFragment();
        initAdapter();

    }


    /**
     * 初始化资源文件及底部tab
     */
    private void initRes() {
        //api19不可用
        if (isManager) {
            image = new int[]{R.mipmap.data,
                    R.mipmap.work,
                    R.mipmap.deal,
                    R.mipmap.address_phone,
                    R.mipmap.mine};
            color = new int[]{ContextCompat.getColor(this, R.color.colorPrimary), ContextCompat.getColor(this, R.color.colorPrimary),
                    ContextCompat.getColor(this, R.color.colorPrimary), ContextCompat.getColor(this, R.color.colorPrimary),
                    ContextCompat.getColor(this, R.color.colorPrimary)};
            strings = new String[]{getString(R.string.title_data),
                    getString(R.string.title_work),
                    getString(R.string.title_dispose),
                    getString(R.string.title_contacts),
                    getString(R.string.title_mine)};
        } else {
            image = new int[]{R.mipmap.work,
                    R.mipmap.deal,
                    R.mipmap.address_phone,
                    R.mipmap.mine};
            color = new int[]{ContextCompat.getColor(this, R.color.colorPrimary),
                    ContextCompat.getColor(this, R.color.colorPrimary), ContextCompat.getColor(this, R.color.colorPrimary),
                    ContextCompat.getColor(this, R.color.colorPrimary)};
            strings = new String[]{
                    getString(R.string.title_work),
                    getString(R.string.title_dispose),
                    getString(R.string.title_contacts),
                    getString(R.string.title_mine)};
        }


    }

    private void initAdapter() {
        myViewPagerAdapter = new MyViewPagerAdapter(this, getSupportFragmentManager(), fragments, strings);
        myVp.setAdapter(myViewPagerAdapter);
        bottomNavigation.setUpWithViewPager(myVp, color, image);
    }

    /**
     * 初始化fragment
     * 如果不是超管也不是店长则不显示数据页面
     */
    private void initFragment() {
        fragments = new ArrayList<>();
        dataFragment = new DataFragment();
        dataFragment.setArguments(bundle);
        workFragment = new WorkFragment();
        workFragment.setArguments(bundle);
        disposeFragment = new DisposeFragment();
        disposeFragment.setArguments(bundle);
        contactsFragment = new ContactsFragment();
        contactsFragment.setArguments(bundle);
        mineFragment = new MineFragment();
        mineFragment.setArguments(bundle);
        if (isManager) {
            fragments.add(dataFragment);
        }
        fragments.add(workFragment);
        fragments.add(disposeFragment);
        fragments.add(contactsFragment);
        fragments.add(mineFragment);
        mineFragment.setLetterAtoF(this);
        initBottom(0);
    }

    /**
     * 初始化bottom
     */
    private void initBottom(int pos) {
        bottomNavigation.selectTab(pos);
        bottomNavigation.isWithText(true);
        bottomNavigation.isColoredBackground(false);
        bottomNavigation.disableViewPagerSlide();


    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bottomNavigation.selectTab(position);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * fragment 回调 关闭当前activity的方法
     */
    @Override
    public void finishActivity() {
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
